package com.smartleanx.module.hrm.app.empRisk;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecord.ExamineRecordDO;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRecord.ExamineRecordMapper;
import com.smartleanx.module.hrm.service.examine.examineRecord.ExamineRecordService;
import org.springframework.stereotype.Component;

import com.smartleanx.framework.common.enums.BizTypeEnum;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.risk.RiskTypeDO;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.eums.employee.BizFileTypeEnum;
import com.smartleanx.module.hrm.eums.risk.RiskLevelEnum;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import com.smartleanx.module.hrm.intergration.FileServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.risk.RiskService;
import com.smartleanx.module.infra.api.file.dto.FileRefQueryDTO;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;

import cn.hutool.core.collection.CollUtil;

/**
 * 低绩效面谈表
 *
 * @author tengcj
 * @since 2024/6/11
 */
@Component
public class RiskLowPerformanceHandleService implements EmployeeRiskHandle {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private FileServiceProvider fileServiceProvider;

    @Resource
    private RiskService riskService;

    @Resource
    private ExamineRecordMapper examineRecordMapper;

    public static final long RISK_ID = RiskTypeEnum.LOW_PERFORMANCE.getCode();

    @Override
    public Long type() {
        return RISK_ID;
    }

    @Override
    public void handleEmployeeRisk(Set<Long> employeeIds) {
        List<EmployeeDO> employeeList = employeeService.getEmployeeList(employeeIds);
        if (CollUtil.isEmpty(employeeList)) {
            riskService.deleteRisk(employeeIds, RISK_ID);
            return;
        }
        RiskTypeDO riskType = riskService.getRiskType(RISK_ID);
        if (riskType == null) {
            throw exception(ErrorCodeConstants.RISK_TYPE_NOT_EXISTS);
        }

        FileRefQueryDTO fileRefQueryDTO = new FileRefQueryDTO();
        fileRefQueryDTO.setBizType(BizTypeEnum.EMPLOYEE.getValue());
        fileRefQueryDTO.setBizFileTypes(Collections.singletonList(BizFileTypeEnum.LOW_PERFORMANCE_INTERVIEW_FORM.getCode()));
        fileRefQueryDTO.setBizRecordIds(employeeList.stream().map(EmployeeDO::getId).collect(Collectors.toSet()));
        List<FileRefRespDTO> fileRefList = fileServiceProvider.getFileRef(fileRefQueryDTO);
        Map<Long, List<FileRefRespDTO>> fileRefMap = CollectionUtils.convertMultiMap(fileRefList, FileRefRespDTO::getBizRecordId);

        //低绩效人员   不过滤文件原因:绩效风险 上传文件  也是null
        List<ExamineRecordDO> examineRecordDOList = new ArrayList<>();
        if(CollUtil.isNotEmpty(employeeIds)){
            examineRecordDOList = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>()
                    .eq(ExamineRecordDO::getIsLowPerformance, "1")
                    .in(ExamineRecordDO::getEmployeeId,employeeIds));
        }

        Map<Integer, List<ExamineRecordDO>> examineMap = examineRecordDOList.stream().collect(Collectors.groupingBy(ExamineRecordDO::getEmployeeId));
//        Map<Integer, ExamineRecordDO> examineMap = examineRecordDOList.stream().distinct().collect(Collectors.toMap(ExamineRecordDO::getEmployeeId, Function.identity()));
        for (EmployeeDO employeeDO : employeeList) {
            List<ExamineRecordDO> list = examineMap.get(employeeDO.getId());
            if (CollUtil.isEmpty(fileRefMap.get(employeeDO.getId())) && CollUtil.isNotEmpty(list)) {
                riskService.addRiskToEmployee(employeeDO.getId(), RISK_ID, RiskLevelEnum.MAJOR.getLevel());
            } else {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
            }
        }
    }

}
